package handler;

import domain.HttpRequest;
import domain.HttpResponse;
import parse.XMLParse;
import pub_enum.FilePath;

import java.lang.reflect.Method;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

public class ReadHandler {

    private SelectionKey selectionKey;
    private Selector selector;

    public ReadHandler() {
    }

    public ReadHandler(SelectionKey selectionKey, Selector selector) {
        this.selectionKey = selectionKey;
        this.selector = selector;
    }

    public SelectionKey getSelectionKey() {
        return selectionKey;
    }

    public void setSelectionKey(SelectionKey selectionKey) {
        this.selectionKey = selectionKey;
    }

    public Selector getSelector() {
        return selector;
    }

    public void setSelector(Selector selector) {
        this.selector = selector;
    }



    public void handle(){
        try {
            HttpRequest httpRequest = new HttpRequest(selectionKey);
            httpRequest.parse();


            SelectableChannel channel = selectionKey.channel();
            if(channel.isOpen()){
                if(httpRequest.getRequestURI().startsWith("/servlet")){

                    //处理动态事件
                    //TODO 封装为方法

                    //分割字符串得到类名
                    /**
                     * String[] split = httpRequest.getRequestURI().split("/");
                     *                     String s = FilePath.SERVLET_PATH.getValue();
                     *                     for (int i = 1; i < split.length; i++) {
                     *                         if(i == split.length - 1 ){
                     *                             s+=split[i];
                     *                         }else {
                     *                             s+=split[i] + ".";
                     *                         }
                     *                     }
                     */

                    //使用XML配置文件获得servlet全类名
                    String s ;
                    if((s = XMLParse.Get(httpRequest.getRequestURI())) == null){
                        s = "servlet.ErrorServlet";
                    }

                    //获取类的实例
                    Class clazz = Class.forName(s);
                    Object o = clazz.newInstance();

                    //通过反射获取service方法
                    Method service = clazz.getDeclaredMethod("service",HttpRequest.class,HttpResponse.class);
                    HttpResponse httpResponse = new HttpResponse(httpRequest);

                    //执行service方法
                    service.invoke(o,httpRequest,httpResponse);




                }else {
                    //获取文件信息
                    HttpResponse httpResponse = new HttpResponse(httpRequest);
                    httpResponse.response();
                    channel.close();
                }
            }else{
                System.out.println("通道已关闭...");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
